<template>
  <div class="menuToggle" @click="toggleMenu"></div>
  <div class="sidebar" :class="{ active: isActive }">
    <div class="uuu">
      <div class="lll logo" @click="this.$router.push({ name: 'Index' })">
        <a>
          <div class="icon">
            <img src="@/assets/cabinet.png" alt="智柜" style="width: 50px; height: 50px;">
          </div>
          <div class="text">智柜</div>
        </a>
      </div>
      <div class="Menulist">
        <div class="lll" v-for="(item, index) in menuItems" :key="index" :style="{ '--bg': item.bgColor }"
          :class="{ active: selectedItem === index }" @click="selectItem(index)">
          <a href="#">
            <div class="icon">
              <el-icon>
                <component :is="iconComponents[item.icon]" />
              </el-icon>
            </div>
            <div class="text">{{ item.text }}</div>
          </a>
        </div>
      </div>
      <div class="bottom">
        <div class="lll">
          <a @click="$router.push('/info/userinfo')">
            <div class="icon">
              <div class="imgBx">
                <img src="@/assets/admin.jpg" alt="User Image" />
              </div>
            </div>
            <div class="text">江南韵</div>
          </a>
        </div>
        <div class="lll" style="--bg:#333">
          <a @click="onLayout">
            <div class="icon">
              <img src="@/assets/exit.svg" alt="退出登录">
            </div>
            <div class="text">退出登录</div>
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  House,
  User,
  Message,
  DataLine,
  ShoppingCart,
  Setting
} from '@element-plus/icons-vue'
import { layout } from '@/utils/layout'
import { showMsg } from '@/utils/showMsg'

export default {
  name: 'LeftNav',
  data()
  {
    return {
      isActive: false,
      selectedItem: 0,
      menuItems: [
        { icon: 'House', text: '首页', bgColor: '#f44336' },
        { icon: 'User', text: '用户管理', bgColor: '#ffa117' },
        { icon: 'Message', text: '站内信', bgColor: '#0fc70f' },
        { icon: 'DataLine', text: '统计', bgColor: '#2196f3' },
        { icon: 'ShoppingCart', text: '订单管理', bgColor: '#b145e9' },
        { icon: 'Setting', text: '系统通知', bgColor: '#e91e63' }
      ],
      // 2. 创建图标组件映射
      iconComponents: {
        House,
        User,
        Message,
        DataLine,
        ShoppingCart,
        Setting
      }
    }
  },
  methods: {
    toggleMenu()
    {
      this.isActive = !this.isActive
    },
    selectItem(index)
    {
      event.preventDefault() // 阻止 a 标签的默认行为
      this.selectedItem = index
      const selectedItem = this.menuItems[index]
      if (this.$router)
      {
        try
        {
          switch (selectedItem.text)
          {
          case '首页':
            this.$router.push({ name: 'adminindex' })
            break
          case '用户管理':
            this.$router.push('/admin/userman')
            break
          case '站内信':
            this.$router.push({ name: 'adminmsg' })
            break
          case '统计':
            this.$router.push({ name: 'statistics' })
            break
          case '订单管理':
            this.$router.push({ name: 'orderman' })
            break
          case '系统通知':
            this.$router.push({ name: 'adminsys' })
            break
          default:
            showMsg('路由错误', '请检查路由参数', 'error')
          }
        }
        catch (error)
        {
          showMsg('路由错误', '跳转失败，请重试', 'error')
        }
      }
      else
      
        showMsg('系统错误', '路由未加载，请刷新页面', 'error')
    },
    onLayout()
    {
      layout(this)
    }
  }
}
</script>

<style scoped>
* {
  user-select: none;
  -webkit-user-drag: none;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --clr: #14161e;
}

body {
  min-height: 100vh;
}

.sidebar {
  position: absolute;
  z-index: 10;
  width: 80px;
  height: 100vh;
  background: #fff;
  transition: 0.5s;
  padding-left: 10px;
  overflow: hidden;
}

.sidebar.active {
  width: 300px;
}

.uuu {
  position: relative;
  height: 100vh;
}

.lll {
  position: relative;
  list-style: none;
}

.lll.active {
  background: var(--clr);
  border-top-left-radius: 50px;
  border-bottom-left-radius: 50px;
  position: relative;
  overflow: hidden;
}

.lll.active::before {
  content: "";
  position: absolute;
  top: -20px;
  right: 0;
  width: 20px;
  height: 20px;
  border-bottom-right-radius: 20px;
  box-shadow: 5px 5px 0 5px var(--clr);
  background: transparent;
}

.lll.active::after {
  content: "";
  position: absolute;
  bottom: -20px;
  right: 0;
  width: 20px;
  height: 20px;
  border-top-right-radius: 20px;
  box-shadow: 5px -5px 0 5px var(--clr);
  background: transparent;
}

.lll.logo {
  margin-bottom: 50px;
}

.lll.logo .icon {
  font-size: 2em;
  color: var(--clr);
}

.lll.logo .text {
  font-size: 1.2em;
  font-weight: 500;
  color: black;
}

.lll a {
  position: relative;
  display: flex;
  white-space: nowrap;
  text-decoration: none;
}

.lll a .icon {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-width: 60px;
  height: 70px;
  font-size: 1.5em;
  color: #333;
  transition: 0.5s;
  padding-left: 10px;
}

.lll a .text {
  position: relative;
  height: 70px;
  display: flex;
  align-items: center;
  font-size: 1em;
  color: #333;
  padding-left: 15px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  transition: 0.5s;
}

.lll.active a .icon {
  color: #fff;
}

.lll.active a .text {
  color: var(--bg);
}

.lll:hover a .icon,
.lll:hover a .text {
  color: var(--bg);
  cursor: pointer;
}

.lll.active a .icon::before {
  content: "";
  position: absolute;
  inset: 5px;
  width: 60px;
  height: 60px;
  background: var(--bg);
  border-radius: 50%;
  transition: 0.5s;
}

.lll:hover.active a .icon::before {
  background: #fff;
}

.bottom {
  position: absolute;
  bottom: 0;
  width: 100%;
}

.imgBx {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
}

.imgBx img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.menuToggle {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 50px;
  height: 50px;
  background: #31a4ff;
  z-index: 1000;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.menuToggle::before {
  content: "";
  position: absolute;
  width: 30px;
  height: 3px;
  background: #fff;
  transform: translateY(-9px);
  transition: 0.5s;
  box-shadow: 0 9px 0 #fff;
}

.menuToggle::after {
  content: "";
  position: absolute;
  width: 30px;
  height: 3px;
  background: #fff;
  transform: translateY(9px);
  transition: 0.5s;
}

.menuToggle.active::before {
  transform: translateY(0) rotate(45deg);
  box-shadow: 0 0 0 #fff;
}

.menuToggle.active::after {
  transform: translateY(0) rotate(-45deg);
}
</style>
